package com.macrog;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * Created by guohong on 2018/6/26.
 */
public class ClientSocket {

    public static final String IP = "localhost";
    public static final int PORT = 667;

    public static void main(String[] args) {

        System.out.println("客户端启动...");

        while (true) {
            Socket socket = null;

            try {
                socket = new Socket(IP, PORT);
                //        socket.setSoTimeout(2000);

                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                String send = "Hello";
                out.writeUTF(send);//客户端发送到服务端
                //        Thread.sleep(2000);
                DataInputStream in = new DataInputStream(socket.getInputStream());
                System.out.println("客户端接收到:" + in.readUTF());
//
                out.close();
                in.close();
            } catch (Exception e) {
                System.out.println("客户端异常");
            } finally {
                if (socket != null) {

                    try {
                        socket.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
    }
}
